{{ define "css"}}
<link href="https://cdn.bootcss.com/limonte-sweetalert2/7.33.1/sweetalert2.min.css" rel="stylesheet">
<style type="text/css">
    body {background: #e5e5e5;}
</style>
{{ end }}

{{ define "content"}}

    <div class="container" style="padding: 2rem 0rem;">
        <div class="columns body-columns">
            <div class="column is-one-quarter">
                <div class="box">
                    {{include "user/sidebar"}}
                </div>
            </div>
            <div class="column">
                <div class="notification is-warning">
                    注意！更新密码以后需要重新登陆。
                </div>
                <div class="card">
                    <header class="card-header">
                        <p class="card-header-title">
                            修改密码
                        </p>
                    </header>
                    <div class="card-content">
                        <div class="content">
                            <form id="app" class="form-signin">
                                <div class="field">
                                    <div class="control">
                                        <label class="label">旧密码</label>
                                        <input class="input" type="password" name="city" v-model="current_password" id="current_password">
                                    </div>
                                </div>
                                <div class="field">
                                    <div class="control">
                                        <label class="label">密码</label>
                                        <input class="input" type="password" name="password" v-model="password" id="password">
                                    </div>
                                </div>
                                <div class="field">
                                    <div class="control">
                                        <label class="label">确认密码</label>
                                        <input class="input" type="password" name="password_confirmation" v-model="password_confirmation" id="password_confirmation">
                                    </div>
                                </div>
                                <button class="button is-block is-info is-medium is-fullwidth" @click="doUpdate">修改密码</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{{ end }}

{{ define "js"}}
<script src="https://cdn.bootcss.com/limonte-sweetalert2/7.33.1/sweetalert2.all.min.js"></script>
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            current_password: '',
            password: '',
            password_confirmation: '',
        },
        delimiters:['${', '}'],
        methods: {
            doUpdate: function (e) {
                e.preventDefault();
                var bodyFormData = new FormData();
                bodyFormData.set('current_password', this.current_password);
                bodyFormData.set('password', this.password);
                bodyFormData.set('password_confirmation', this.password_confirmation);
                let config = {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                };
                this.$http.post("/settings/password", bodyFormData, config).then((response) => {
                    console.log(response);
                    if (response.data.code === 0) {
                        // http://mishengqiang.com/sweetalert/
                        swal("更新成功！", "","success");
                        setTimeout(function () {
                            window.location.href = "/login";
                        }, 2000);
                        return true;
                    } else {
                        swal("修改失败！", "","error");
                        return false;
                    }
                }).catch( error => {
                    console.log(error);
                });
            }
        }
    })
</script>
{{ end }}